<template>
  <div>
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="工单号">
                <a-input placeholder="请输入工单号" v-model.trim="queryParam.workOrderBarCodeLike"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="工位">
                <j-dict-select-tag placeholder="请选择工位" v-model="queryParam.stationId"
                                   dictCode="mes_base_factory,factory_name,id,factory_status=1 and factory_level=4"
                                   :use-cache="false"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :loading="loading"
          :pagination="ipagination"
          class="j-table-force-nowrap"
          @change="handleTableChange"
        >
          <template slot="value" slot-scope="text, record,index">
            <a-form-model-item>
              <a-input v-model="record.replaceMaterialBarCode"/>
            </a-form-model-item>
          </template>
          <template slot="action" slot-scope="text, record">
            <a @click="replaceMaterialBarCode(record)">确认</a>
          </template>
        </a-table>
      </div>
    </a-card>
  </div>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  //列表API
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'

  export default {
    name: 'ProductionOnlineReplacement',
    mixins: [JeecgListMixin, mixinDevice],
    data() {
      return {
        url: {
          list: '/mes/productionOnlineReplacement/queryProductionOnlineReplacementList',
          replaceMaterialBarCode: '/mes/productionOnlineReplacement/replaceMaterialBarCode'
        },

        queryParam: {
          workOrderBarCodeLike: ''
        },
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '物料号',
            align: 'center',
            dataIndex: 'materialBarCode'
          },
          {
            title: '物料编码',
            align: 'center',
            dataIndex: 'materialCode'
          },
          {
            title: '替换物料号',
            dataIndex: 'replaceMaterialBarCode',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: { customRender: 'value' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: { customRender: 'action' }
          }
        ]
      }
    },
    created() {
    },
    methods: {
      replaceMaterialBarCode(record) {
        let materialSequence = record.materialSequence
        let replaceMaterialBarCode = record['replaceMaterialBarCode']
        if (replaceMaterialBarCode.indexOf(materialSequence) === -1) {
          this.$message.warning('替换的物料号不正确，请修正后再操作')
          return
        }
        let materialBarCode = record.materialBarCode
        if (materialBarCode === replaceMaterialBarCode) {
          this.$message.warning('替换的物料号和之前的一致，请修正后在操作')
          return
        }
        this.loading = true
        postAction(this.url.replaceMaterialBarCode, {
          materialBarCodeOld: materialBarCode,
          materialBarCodeNew: replaceMaterialBarCode
        }).then((res) => {
          this.loading = false
          if (res.success) {
            this.loadData(this.ipagination.current);
          } else {
            this.$message.warning(res.message)
          }
        }).catch((err) => {
          this.$message.warning(err.message)
        })
      }
    }
  }
</script>

<style scoped>

</style>